package com.lepu.app.fun.tool.cjsj.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.DateUtil;
import com.app.utils.LoginUtil;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.ObserverManager;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.cjsj.ChanJianAddActivity;
import com.lepu.app.fun.tool.cjsj.bean.BeanToolChanJian;
import com.lepu.app.fun.tool.ymsjb.remind.Alarm;
import com.lepu.app.fun.tool.ymsjb.remind.AlarmTipsManager;
public class ToolChanJianAdapter extends BaseAdapter implements AsyncRequest {
	private String REQUEST_DATA = "request_data";
	
	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	public ArrayList<BeanToolChanJian> mArrayList = null;
	public String [] mDateArray = null;

	public ToolChanJianAdapter(Context context,ArrayList<BeanToolChanJian> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mDateArray = mContext.getResources().getStringArray(R.array.array_cjsj_date);
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			
		    convertView = mLayoutInflater.inflate(R.layout.tool_chanjian_item, null);

			holder = new ViewHolder();

			TextView dateTextView = (TextView) convertView.findViewById(R.id.dateText);
			TextView remindTextView = (TextView) convertView.findViewById(R.id.remindText);
			TextView noteTextView = (TextView) convertView.findViewById(R.id.noteText);
			ImageView editImageView = (ImageView) convertView.findViewById(R.id.editImage);
			ImageView deleteImageView = (ImageView) convertView.findViewById(R.id.deleteImage);

			holder.dateTextView = dateTextView;
			holder.remindTextView = remindTextView;
			holder.noteTextView = noteTextView;
			holder.editImageView = editImageView;
			holder.deleteImageView = deleteImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanToolChanJian item = mArrayList.get(position);
		
		holder.dateTextView.setText(DateUtil.getHMTime(item.date));
		if(item.remindIs == 0){
			holder.remindTextView.setText("无");
		}else if(item.remindIs == 1){
			holder.remindTextView.setText(mDateArray[item.remindIndex]+" "+item.remindTime);
		}
		
		holder.noteTextView.setText(item.note);
		
		holder.editImageView.setOnClickListener(new MyViewOnclicklistener(item));
		holder.deleteImageView.setOnClickListener(new MyViewOnclicklistener(item));
		
		
		return convertView;
	}
	
	class MyViewOnclicklistener implements View.OnClickListener {
	        private BeanToolChanJian bean;

	        public MyViewOnclicklistener(BeanToolChanJian alarm) {
	            this.bean = alarm;
	        }

	        @Override
	        public void onClick(View v) {
	            final int rid = v.getId();
	            switch (rid) {
	                case R.id.editImage:
	                	Intent intent = new Intent(mContext,ChanJianAddActivity.class);
	                	Bundle bundle = new Bundle();
	                	bundle.putSerializable("chanjian", bean);
	                	intent.putExtras(bundle);
	                	((BaseFragmentActivity)mContext).startActivity(intent,true);
	                    break;
	                case R.id.deleteImage:
	                	deleteRemind(bean);
	                	break;
	                default:
	                    break;
	            }
	        }
	    }
	
	public void deleteRemind(final BeanToolChanJian bean) {
			
			String message = "您确定删除该记录吗？";
			MyDialog dialog = new MyDialog((BaseFragmentActivity)mContext)
					.setMessage(message)
					.setNegativeButton(R.string.app_cancel,
							R.drawable.btn_cancel_selector, R.color.color_white,
							new View.OnClickListener() {

								@Override
								public void onClick(View v) {

								}
							})
					.setPositiveButton(R.string.app_ok,
							R.drawable.btn_ok_selector, R.color.color_white,
							new View.OnClickListener() {

								@Override
								public void onClick(View v) {
			                        //删除提醒
			                    	Alarm.closeRemind(MyApplication.alarmManager, mContext, bean.remindId);
			                        AlarmTipsManager.deleteItem(bean.remindId); 
									
			                        request(bean.id);
								}
							});

			dialog.create(null);
			dialog.showMyDialog();
		}

	
    public void request(int id) {
			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject params = new JSONObject();
			try {
				params.put("DeviceID", Setting.DEVICEID);
				params.put("ApplicationID", Setting.APPLICATIONID);
				
				params.put("UserID", LoginUtil.getUserId());
				params.put("LoginToken", LoginUtil.getLoginToken());
				
				params.put("UPCID", id);

			} catch (Exception e) {
			}
			map.put("ht", params);

			ApiClient.http_post_main(Setting.getDeleteChanjianUrl(), map, null, this,REQUEST_DATA, true, false);
		}	
		
	private static class ViewHolder {
		TextView dateTextView;
		TextView remindTextView;
		TextView noteTextView;
		ImageView editImageView;
		ImageView deleteImageView;
	}

	@Override
	public void RequestComplete(Object arg0, Object arg1) {
		ObserverManager.getInstance().notify(ObserverConst.NOTIFY_CHANJIAN_CHANGE, null,null);
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		
	}
}
